let baseURL = 'https://fd.qianyanwangluo.com';
let ACCOUNTINFO = 'accountinfo';
import {
	Local
} from "@utils/storage";
let user = null;
// "Content-Type": "application/json;charset=UTF-8"
// 缓存key和tokenkey属于自定义
const request = (config) => {
	config.url = baseURL + config.url;
	config.header = {};
	user = Local.get(ACCOUNTINFO);
	if (user) config.header['token'] = user.token;
	if (!config.data) config.data = {};
	return new Promise(function(resolve, reject) {
		uni.request(config).then(responses => {
			let data = responses.data; // 去掉一层data
			if (data.code == 1) {
				resolve(data);
			} else {
				if (data.code == 401) {
					let pages = getCurrentPages();
					if (pages[0].route != 'pages/login/login') {
						uni.reLaunch({
							url: '/pages/login/login'
						})
					}
					Local.remove(ACCOUNTINFO);
					uni.showToast({
						title: '登录后操作',
						icon: 'none'
					})
				} else if (data.hasOwnProperty('msg')) {
					uni.showToast({
						title: data.msg,
						icon: 'none'
					})
				} else {
					console.log('未配置')
				}
				reject(data);
			}
		}).catch(error => {
			uni.showToast({
				title: '请求失败',
				icon: 'none'
			})
			reject(error);
		})
	})
};
export {
	baseURL,
	user
};
export default request;